package com.example.demo;

import java.io.FileNotFoundException;
import java.text.ParseException;

import com.example.demo.repository.AutoMessageHandler;
import com.example.demo.service.MonitoringMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.normalClasses.eye.FileManipulation;
import com.normalClasses.eye.Queue_message_handler;
import com.normalClasses.eye.monitor_res_obj;

@Slf4j
@Component
public class AutoRealTimeMonitoring {
    //装配监控函数
    @Autowired
    private MonitoringMethod mon;
    //装配内部接口，用于访问数据库
    @Autowired
    private AutoMessageHandler messageHandler;
    //消息待处理队列
    public static Queue_message_handler<monitor_res_obj> monitoring_message = new Queue_message_handler();

    //消息处理器，阻塞
    @Scheduled(fixedDelay = 150)
    public void message_handler() {
        monitor_res_obj user = null;
        try {
            user = monitoring_message.pop();
        } catch (Exception e) {
            //TODO Auto-generated catch block
            log.error(LogUtil.logSpec(LogUtil.logSpec("monitoring_message maybe has pop problem, or system forced shutdown")));
        }
        if (user != null && user.getExcp_num() > 20) {
        	//群发给管理员
            mon.Handle_abnormal_behavior(user.getMessage(), "template1.html");
        } else if (user != null) {
        	//单发给个人用户
            mon.Send_abnormal_behavior(user.getMessage(), "template3.html", user.getUserid());
        }

    }

    //分片暂时存储目标log
    private final String log1 = "log.log";
    //源log文件
    private final String log2 = "C:/log.log";
    //行为分析装配
    @Autowired
    private Analysis Analysis;

    //日志监控系统，间隔20分钟处理一次
    @Scheduled(fixedDelay = 20 * 60 * 1000)
    public void log_handler() {
        //从源log文件中剥离待处理的目标文件，避免读写冲突
        FileManipulation.getNewLogPart(log2, log1);
        //记录一条分割线,使得源log总存在最后一行
        log.info(LogUtil.logSpec(LogUtil.logSpec("log_handler carry out once")));
        //获得username列表
        String[] usernames = messageHandler.Auto_find_username();
        //遍历进行异常行为分析
        for (String username : usernames) {
            try {
                //判断登陆异常
                String login_inf = Analysis.getLoginBehaviors(username, log1);
                if (!login_inf.equals("")) {
                    //将异常处理
                    mon.Make_adjude_behavior(username, 21, login_inf);
                }
                //判断多次检查cookie接口的刷新异常
                String cookie_inf = Analysis.getCookieBehaviors(username, log1);
                if (!cookie_inf.equals("")) {
                    //将异常处理
                    mon.Make_adjude_behavior(username, 22, cookie_inf);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //每次执行方法，检查一下error级别的容错程度，如果超出则需要通报处理,数字10可修改
        try {
            String[] errorlist = Analysis.getErrorBehaviors(log1);
            if (errorlist.length > 10) {
                //为处理队列新加入一条,第一个参数可以不传，第二个参数得对应住，第三个也只是起到占位符作用，无实际用处
                monitor_res_obj msg = new monitor_res_obj("", 23, 0);
                //为消息处理主体添加信息
                for (String item : errorlist) {
                    msg.appendMessage(item + "\n");
                }
                //添加到处理队列
                try {
                    monitoring_message.push(msg);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//每次执行方法，检查一下warn级别的容错程度，如果超出则需要通报处理,数字20可修改  
        try {
            String[] warnlist = Analysis.getWarningBehaviors(log1);
            if (warnlist.length > 20) {
                //为处理队列新加入一条,第一个参数可以不传，第二个参数得对应住，第三个也只是起到占位符作用，无实际用处
                monitor_res_obj msg = new monitor_res_obj("", 24, 0);
                //为消息处理主体添加信息
                for (String item : warnlist) {
                    msg.appendMessage(item + "\n");
                }
                //添加到处理队列
                try {
                    monitoring_message.push(msg);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
